define(["../common"], function() {

	var saveStorePath = g_firmDomain + "/store/addOrEditDo/json";
	var addSMPath = g_firmDomain + "/storeMember/addDo/json";
	var delDoPath = g_firmDomain + "/storeMember/delDo/json";
	var loadRolesPath = g_firmDomain + "/store/loadRoles/json";
	
	require(["region"], function() {
		//加载地区并生成下拉列表
		$("#regions").regionsSelect({
			value: $("#regionId").val(),
			isShowNull: true,
			nullName: "-选择-",
			onLoad: function($select) {
				$select.attr("data-rule","required;");
			},
			onSelect: function($select) {
				$("#regionId").val($select.val());
				var regionPath = $("[id^='selectRegion_'] option:selected").map(function(){
					return $(this).text();
				}).get().join(" ");
				$("#regionPath").val(regionPath);
			}
		});
	});
	
	loadjs(["validator"],function() {
		// 店铺表单
		$("#storeForm").validator({
			fields: {
				name: "required;length[3~50]",
				address: "required;length[3~80]",
				contacts: "length[~20]",
				phone: "phone"
			}
		});
		
		// 管理员表单
		$("#storeMemberForm").validator({
			fields: {
				userName: {
					rule: "required",
					target: "#addMemMsg"
				},
				role: {
					rule: "required",
					target: "#addMemMsg"
				},
				storeId: {
					rule: "required",
					target: "#addMemMsg",
					msg: {
						required: "请先保存店铺信息"
					}
				}
			}
		});
	});

	//事件：删除店铺管理员
	$(document).on('click', "[id^='del_']", function() {
		var memberId = ($(this).attr("id").split("_"))[1];
		var storeId = $("[name='storeId']").val();
		var $item = $("#item_" + memberId);
		if ($item.doing()) return;
		
		var msgboxConfig = {
			id: "del_" + memberId,
			type: 'confirm',
			content: "确定要删除吗？",
			title: "提示",
			onClose: onClose_
		};
		
		loadjs(["msgbox"], function() {
			$.msgbox(msgboxConfig);
		});
		
		function onClose_() {
			if (!this.val()) {
				return;
			}
			$item.showMsg("删除用户...", 'load', true);
			$item.fadeTo(null,0.1);
			// (ie兼容) 加 r=Math.random()
			$.getJSON(delDoPath + "?r=" + Math.random(), {storeId: storeId, memberId: memberId}, function(data) {
				if (data.status === 0) {
					$item.showMsg("删除成功！", 'pass');
					$item.fadeOut(function() {$(this).remove();});
				} else {
					$item.showMsg(data.message, 'error');
					$item.fadeTo('fast',1);
				}
			});
		}
		
	});
	
	$("#storeForm").on('valid.form', function(e, form) {
		if (!$(form).showMsg("保存店铺...", 'load', true)) return;
		
		$.post(saveStorePath, $(form).serialize(), function(data) {
			if (data.status !== 0) {
				$(form).showMsg(data.message, 'error');
				return;
			}
			
			$(form).showMsg("保存成功！", 'pass');
			var storeId = data.result;
			
			if (!$("[name='storeId']").val()) {
				$("[name='storeId']").val(storeId);
				// 加载店铺管理角色
				loadRoles(loadRolesPath, storeId, form);
			}
		});
	});
	
	$("#storeMemberForm").on('valid.form', function(e, form) {
		if (!$(form).showMsg("添加用户...", 'load', true)) return; 
		
		$.post(addSMPath, $(form).serialize(), function(data) {
			if (data.status !== 0) {
				$(form).showMsg(data.message, 'error');
				return;
			}
			
			$(form).showMsg("添加成功！", 'pass');
			var storeMember = data.result;
			var realName = !storeMember.realName ? "" : storeMember.realName;
			var mobile = !storeMember.mobile ? "" : storeMember.mobile;
			$("#storeMemberTable").append("<tr id='item_" + storeMember.memberId + "'><td><a href='#' id='sm_" + storeMember.name + "'>" + storeMember.name + "</a></td><td>" + realName + "</td><td>" + mobile + "</td><td class='cz'><a href='javascript:;' id='del_" + storeMember.memberId + "'>删除</a></td></tr>");
		
		});
	})

	// 加载店铺管理角色
	function loadRoles(loadRolesPath, storeId, form) {
		if ($("[name='roleId']").children().size() > 0) {
			return;
		}
		$.post(loadRolesPath, {storeId: storeId}, function(data) {
			if (data.status !== 0) {
				$(form).showMsg(data.message, 'error');
				return;
			}
			var roles = data.result;
			var option = "";
			for (var i = 0; i < roles.length; i++) {
				option += "<option value='" + roles[i].roleId + "'>" + roles[i].name + "</option>";
			}
			$("[name='roleId']").append(option);
		});
	}

});
